package com.zsoft.webmail.servlet.hainannet;

import javax.servlet.ServletException;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
import com.zsoft.webmail.MemberAuthorization;
import com.zsoft.webmail.AuthBean;
import com.zsoft.webmail.Application;
import comm.util.Utils;
import com.zsoft.webmail.filter.*;
import com.zsoft.webmail.folder.*;


public class FilterChangeServlet extends HttpServlet {
  //private static final String CONTENT_TYPE = Application.getProperty("ContentType");
  private static final String CONTENT_TYPE = "text/html";
  /**Initialize global variables*/
  public void init() throws ServletException {
  }
  /**Process the HTTP Get request*/
  public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    response.setContentType(CONTENT_TYPE);
    comm.http.Request request1=new comm.http.Request(request,Application.getProperty("defaultInputCharset"));

    //PrintWriter out = response.getWriter();
    String mimeCharset1=Application.getProperty("defaultMimeCharset");
    if(mimeCharset1==null || mimeCharset1.equals(""))
    mimeCharset1 = com.zsoft.webmail.mimeutility.MimeUtility.mimeCharset(com.zsoft.webmail.mimeutility.MimeUtility.getDefaultJavaCharset());
    String javaCharset1=com.zsoft.webmail.mimeutility.MimeUtility.javaCharset(mimeCharset1);
    comm.http.ResponseOut out= new comm.http.ResponseOut(response,javaCharset1,false);


    MemberAuthorization memberAuthToken=null ;
    HttpSession session=request.getSession();
    Object object = session.getAttribute("authBean");
    if(object==null){
      session.setAttribute("authBean",new com.zsoft.webmail.AuthBean(session.getId(),request.getRemoteAddr()));
      response.sendRedirect( "login.jsp" );
      return;
    }else{
      memberAuthToken = ((AuthBean)object).getMemberAuthToken();
      if(memberAuthToken==null){
        response.sendRedirect( "login.jsp" );
        return;
      }
    }






Filter filtertmp=null;
try{
filtertmp=new Filter(memberAuthToken.homePath);
FilterItem itemtmp=new FilterItem();
for(int i=0;i<6;i++)
{
	if(request1.getParameter("checkbox"+i)!=null)
	{
		switch(i)
		{
		case 0:
		if(request1.getParameter("from_textfield").length()!=0)
			itemtmp.addCondition(0,Integer.parseInt(request1.getParameter("select_from")),request1.getParameter("from_textfield"));
		break;
		case 1:
		if(request1.getParameter("to_textfield").length()!=0)
			itemtmp.addCondition(1,Integer.parseInt(request1.getParameter("select_to")),request1.getParameter("to_textfield"));
		break;
		case 2:
		if(request1.getParameter("sub_textfield").length()!=0)
			itemtmp.addCondition(2,Integer.parseInt(request1.getParameter("select_sub")),request1.getParameter("sub_textfield"));
		break;
		case 5:
		if(request1.getParameter("cont_textfield").length()!=0){
                    String temCont=request1.getParameter("cont_textfield");
                    if(temCont==null)temCont="";
                    if(temCont.length()!=0)
                    {

                            int pos;
                            int index=0;
                            String head,tail;
                            while((pos=temCont.indexOf('\r'))!=-1)
                            {
                                    head=temCont.substring(0,pos);
                                    tail=temCont.substring(pos+1,temCont.length());
                                    temCont=head+"\\r"+tail;
                            }
                            while((pos=temCont.indexOf('\n'))!=-1)
                            {
                                    head=temCont.substring(0,pos);
                                    tail=temCont.substring(pos+1,temCont.length());
                                    temCont=head+"\\n"+tail;
                            }
                            while((pos=temCont.indexOf('\"',index))!=-1)
                            {
                                    head=temCont.substring(0,pos);
                                    tail=temCont.substring(pos+1,temCont.length());
                                    temCont=head+"\\\""+tail;
                                    index=pos+2;
                            }
                    }
                    itemtmp.addCondition(5,Integer.parseInt(request1.getParameter("select_cont")),temCont);
                }
                break;
		case 3:
		if(request1.getParameter("size_textfield").length()!=0)
			itemtmp.addCondition(3,Integer.parseInt(request1.getParameter("select_size")),request1.getParameter("size_textfield"));
		break;
		case 4:
			itemtmp.addCondition(4,0,"");
		break;
		default:
		}
	}
}
String itemname=request1.getParameter("itemname");
itemname=itemname.trim();
itemtmp.addItemName(itemname);
int action=Integer.parseInt(request1.getParameter("radiobutton"));
String mailto_txt,returnto_txt,boxto_txt;
mailto_txt=null;
returnto_txt=null;
boxto_txt=null;
if(action==3)
	if(request1.getParameter("boxto_text").length()!=0)
		boxto_txt=request1.getParameter("boxto_text");
if(action==1)
	if(request1.getParameter("mailto_text").length()!=0)
		mailto_txt=request1.getParameter("mailto_text");
if(action==2)
	if(request1.getParameter("returnto_text").length()!=0)
	{
		returnto_txt=request1.getParameter("returnto_text");
		int pos;
		int index=0;
		String head,tail;
		while((pos=returnto_txt.indexOf('\r'))!=-1)
//		if((pos=returnto_txt.indexOf('\r'))!=-1)
		{
			head=returnto_txt.substring(0,pos);
			tail=returnto_txt.substring(pos+1,returnto_txt.length());
			returnto_txt=head+"\\r"+tail;
		}
		while((pos=returnto_txt.indexOf('\n'))!=-1)
//		if((pos=returnto_txt.indexOf('\n'))!=-1)
		{
			head=returnto_txt.substring(0,pos);
			tail=returnto_txt.substring(pos+1,returnto_txt.length());
			returnto_txt=head+"\\n"+tail;
		}
		while((pos=returnto_txt.indexOf('\"',index))!=-1)
		{
			head=returnto_txt.substring(0,pos);
			tail=returnto_txt.substring(pos+1,returnto_txt.length());
			returnto_txt=head+"\\\""+tail;
			index=pos+2;
		}

	}

if(action==0)
	itemtmp.addAction(0);
else if((action==1)&&(mailto_txt!=null))
{
	itemtmp.addAction(1);
        //if(mailto_txt.length() > 200){
        //  mailto_txt=mailto_txt.substring(0,200);
        //}
	itemtmp.addActionVal(mailto_txt);
}
else if((action==2)&&(returnto_txt!=null))
{
	itemtmp.addAction(2);
	itemtmp.addActionVal(returnto_txt);
}
else if((action==3)&&(boxto_txt!=null))
{
	itemtmp.addAction(3);
        String fPath="/inbox/new";
        try{
          ListFolders listfolder= new ListFolders(request);
          fPath=listfolder.getFolderPath(boxto_txt);
          if(fPath==null)fPath="/inbox/new";
        }catch(Exception e){
          fPath="/inbox/new";
        }
	itemtmp.addActionVal(fPath);
}
//out.print(filtertmp.getItemCount());
//out.print(itemtmp.getCondCount());

//out.print(itemtmp.Action);
//out.print(itemtmp.ActionVal);

out.println("<meta http-equiv=\"Content-Type\" content=\"text/html; charset="+mimeCharset1+"\">");
if(!filtertmp.changeItem(itemtmp))
{

//response.sendRedirect("FilterChangeItemErr.jsp");
                out.println("error");
                return;
}else{
  response.sendRedirect("Filter?itemname="+com.zsoft.webmail.URLEncoder.encode(comm.util.StringUtils.moveNull(itemname),javaCharset1)+"&mode=0&index=1");


}





out.println("<html>");
out.println("<body>");
out.println("</body>");
out.println("</html>");

}catch(Exception ex){
  throw new ServletException(ex);
}finally{
  try{
    filtertmp.close();
  }catch(Exception e){}
}


  }
  /**Process the HTTP Post request*/
  public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    doGet(request,response);
  }
  /**Clean up resources*/
  public void destroy() {
  }
}